package com.dm.wallpaper.library.ablility.slice;

import com.dm.wallpaper.library.ResourceTable;
import com.dm.wallpaper.library.ablility.WallpaperAbility;
import com.dm.wallpaper.library.fraction.*;
import com.dm.wallpaper.library.utils.Log;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

public class WallpaperBoardAbilitySlice extends AbilitySlice {

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
//        this.setDisplayOrientation(AbilityInfo.DisplayOrientation.PORTRAIT);
        DirectionalLayout directionalLayout = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_ability_wallpaper_board_onelayout, null, false);
        setUIContent(directionalLayout);
    }


    @Override
    public void onActive() {
        Log.i("WallpaperBoardAbilitySlice.onActive([]):26");
        super.onActive();
        FractionHelper.getInstance((FractionAbility) getAbility()).addFraction(ResourceTable.Id_main_layout, CollectionWallpaperFraction.class);
        FractionHelper.getInstance((FractionAbility) getAbility()).addFraction(ResourceTable.Id_main_layout, FavoritesFraction.class);
        FractionHelper.getInstance((FractionAbility) getAbility()).addFraction(ResourceTable.Id_main_layout, SettingsFraction.class);
        FractionHelper.getInstance((FractionAbility) getAbility()).addFraction(ResourceTable.Id_main_layout, AboutFraction.class);
        if (getAbility() instanceof WallpaperAbility) {
            WallpaperAbility ability = (WallpaperAbility) getAbility();
            if (ability.getShowTag() == ResourceTable.Id_favorites) {
                FractionHelper.getInstance((FractionAbility) getAbility()).showFraction(FavoritesFraction.class);
            } else if (ability.getShowTag() == ResourceTable.Id_settings) {
                FractionHelper.getInstance((FractionAbility) getAbility()).showFraction(SettingsFraction.class);
            } else if (ability.getShowTag() == ResourceTable.Id_wallpapers) {
                FractionHelper.getInstance((FractionAbility) getAbility()).showFraction(CollectionWallpaperFraction.class);
            } else if (ability.getShowTag() == ResourceTable.Id_about) {
                FractionHelper.getInstance((FractionAbility) getAbility()).showFraction(AboutFraction.class);
            }
        }
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onInactive() {
        super.onInactive();
    }

    @Override
    protected void onBackground() {
        super.onBackground();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onBackPressed() {
        Fraction fraction = FractionHelper.getInstance((FractionAbility) getAbility()).gotoLastFraction();
        WallpaperAbility ability = (WallpaperAbility) getAbility();
        if (fraction != null) {
            if (fraction instanceof FavoritesFraction) {
                ability.setShowTag(ResourceTable.Id_favorites);
            } else if (fraction instanceof SettingsFraction) {
                ability.setShowTag(ResourceTable.Id_settings);
            } else if (fraction instanceof CollectionWallpaperFraction) {
                ability.setShowTag(ResourceTable.Id_wallpapers);
            } else if (fraction instanceof AboutFraction) {
                ability.setShowTag(ResourceTable.Id_about);
            }
        } else {
            super.onBackPressed();
        }
    }
}
